package com.hy.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hy.admin.dao.CourseCommentDao;
import com.hy.admin.dao.CourseDao;
import com.hy.admin.dao.MemberDao;
import com.hy.admin.entity.CourseCommentEntity;
import com.hy.admin.entity.CourseEntity;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.helper.response.CommentResponse;
import com.hy.admin.service.CommentService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CourseCommentDao courseCommentDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private CourseDao courseDao;

    @Override
    public ServicesResult<List<CommentResponse>> commentList(
            Long courseId, Integer pageNum, Integer pageSize) {
        ServicesResult<List<CommentResponse>> servicesResult = new ServicesResult<>();
        //校验请求参数
        if (courseId == null || courseId < 0){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        //查询评论数据
        //设置分页条件，Parameters:pageNum 页码pageSize 每页显示数量count 是否进行count查询
        if(pageNum == null || pageNum <= 0){
            //初始页码为1
            pageNum = 1;
        }
        if(pageSize == null || pageSize <= 0){
            //初始数量为20
            pageSize = 20;
        }
        CourseEntity course = courseDao.selectByPrimaryKey(courseId);
        if(course == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("课程不存在");
            return servicesResult;
        }
        List<CommentResponse> courseList;
        PageHelper.startPage(pageNum, pageSize);
        if(course.getSeriesId() != null && course.getSeriesId() > 0){
            courseList = courseCommentDao.selectBySeriesId(course.getSeriesId());
        }else{
            courseList = courseCommentDao.selectByCourseId(courseId);
        }
        servicesResult.setResult(courseList);
        return servicesResult;
    }

    @Override
    public ServicesResult<Boolean> addComment(CourseCommentEntity comment) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<>();
        //校验请求参数
        if (comment == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        //校验必填参数
        if(comment.getCourseId() == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("courseId不能为空");
            return servicesResult;
        }
        if(comment.getMemberId() == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("memberId不能为空");
            return servicesResult;
        }
        if(comment.getContent() == null || "".equals(comment.getContent())){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("评论内容不能为空");
            return servicesResult;
        }
        MemberEntity member = memberDao.selectById(comment.getMemberId());
        if(member == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户不存在");
            return servicesResult;
        }
        if(member.getMemberType() == null){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户信息不完善");
            return servicesResult;
        }
        Calendar cal = Calendar.getInstance();
        comment.setCreateTime(cal.getTime());
        comment.setUpdateTime(cal.getTime());
        //插入数据
        int result = courseCommentDao.insertSelective(comment);
        servicesResult.setResult(result == 1 ? true : false);
        return servicesResult;
    }

    @Override
    public Integer commentCount(Long courseId) {
        CourseEntity course = courseDao.selectByPrimaryKey(courseId);
        if(course.getSeriesId() != null && course.getSeriesId() > 0){
            return courseCommentDao.selectCountBySeriesId(course.getSeriesId());
        }else{
            return courseCommentDao.selectCountByCourseId(courseId);
        }
    }
}
